package ua.kharkov.knure.pereverziev.diploma.web.command;

import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import ua.kharkov.knure.pereverziev.diploma.web.command.admincommand.AddAirportCommand;
import ua.kharkov.knure.pereverziev.diploma.web.command.admincommand.Administration;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.AjaxOnlineDesc;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.AjaxTimetable;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.AjaxUserChange;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.FlightsCreate;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.GetEmptySits;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.GetPrice;
import ua.kharkov.knure.pereverziev.diploma.web.command.ajax.PasswordRecovery;

/**
 * Holder for all commands.<br/>
 * 
 * @author A.Pereverziev
 * 
 */
public class CommandsContainer {

	private static final Logger log = Logger.getLogger(CommandsContainer.class);
	private static CommandsContainer instance = null;
	protected Map<String, Class> commands = defaultMap();
	protected Map<String, Command> commandInstances = new TreeMap<String, Command>();

	private CommandsContainer() {
	}

	public static synchronized CommandsContainer getInstance() {
		if (instance == null) {
			instance = new CommandsContainer();
		}
		return instance;
	}

	public Command get(final String commandName) {
		log.debug("Trying to get command class " + commandName);
		Command commandInstance = commandInstances.get(commandName);
		if (commandInstance == null) {
			log.debug("Creating command class for " + commandName);
			final Class klass = commands.get(commandName);
			if (klass == null)
				throw new NullPointerException(getClass()
						+ " was unable to find an action named '" + commandName
						+ "'.");

			try {
				commandInstance = (Command) klass.newInstance();
			} catch (final Exception e) {
				log.error("Error while instantinating klass" + klass);
				e.printStackTrace();
			}
			commandInstances.put(commandName, commandInstance);
		}
		return commandInstance;
	}

	protected Map<String, Class> defaultMap() {
		final Map<String, Class> commands = new TreeMap<String, Class>();
		System.out.println(AirportDateChoiceCommand.class);
		commands.put("airportDatePointChoice", AirportDateChoiceCommand.class);
		commands.put("changeLocale", ChangeLocaleCommand.class);
		commands.put("checkOut", CheckOutCommand.class);
		commands.put("login", LoginCommand.class);
		commands.put("logout", LogoutCommand.class);
		commands.put("registration", RegistrationCommand.class);
		commands.put("returnFlightChoice", ReturnFlightChoiceCommand.class);
		commands.put("personalData", PersonalDataCommand.class);
		commands.put("showOrders", ShowOrdersCommand.class);
		commands.put("addairport", AddAirportCommand.class);
		commands.put("goto", GotoCommand.class);
		commands.put("secure_goto", SecureGotoCommand.class);
		commands.put("ajax_timetable", AjaxTimetable.class);
		commands.put("ajax_online_desc", AjaxOnlineDesc.class);
		commands.put("ajax_get_empty_sits", GetEmptySits.class);
		commands.put("ajax_get_price", GetPrice.class);
		commands.put("create_ticket", CreateTicketCommand.class);
		commands.put("administration", Administration.class);
		commands.put("password_recovery", PasswordRecovery.class);
		commands.put("ajax_user_change", AjaxUserChange.class);
		commands.put("create_flights", FlightsCreate.class);
		commands.put("go_back", GoBack.class);
		commands.put("get_report", GetReport.class);
		commands.put("get_pdf_report", GetPdfReport.class);
		return commands;
	}

}
